/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sistema.entidades;

import br.com.sistema.excecoes.EntidadeNaoEncontrada;
import br.com.sistema.infraestrutura.ConnectionPool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 *
 * @author Lauro
 */
public class Menu {
    private int id;
    private String nome;
    private int indice;
    private List<ItemMenu> itensMenu;
    protected Connection conexao;

    public Menu(int id, String nome, int indice) {
        this.id = id;
        this.nome = nome;
        this.indice = indice;
    }

    public Menu(String nome) {
        this.nome = nome;
    }
    
    //<editor-fold defaultstate="collapsed" desc="Metodos Getters & Setters.">
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getIndice() {
        return indice;
    }

    public void setIndice(int indice) {
        this.indice = indice;
    }

    public List<ItemMenu> getItensMenu() throws SQLException {
        if(itensMenu == null){
            itensMenu = ItemMenu.getItemMenu(this);
        }
        return itensMenu;
    }

    public void setItensMenu(List<ItemMenu> itensMenu) {
        this.itensMenu = itensMenu;
    }

    public Connection getConexao() {
        if(conexao == null){
            this.conexao = ConnectionPool.getConnection();
        }
        return conexao;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Strings SQL.">
    private String getSQLInsercao() {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO menu ( ");
        sql.append("   nome, ");
        sql.append("   indice ");
        sql.append(" ) VALUES ( ");
        sql.append("   ?, ");
        sql.append("   ? ");
        sql.append(" ) ");
        return sql.toString();
    }
    
    private String getSQLAtualizacao() {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE ");
        sql.append(" menu ");
        sql.append(" SET ");
        sql.append(" nome=?, ");
        sql.append(" indice=? ");
        sql.append(" WHERE id = ? ");
        return sql.toString();
    }
    
    private String getSQLExclusao() {
        StringBuilder sql = new StringBuilder();
        sql.append(" DELETE FROM ");
        sql.append(" menu ");
        sql.append(" WHERE id = ? ");
        return sql.toString();
    }
    
    protected String getSQLBuscarPorId() {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * ");
        sql.append(" FROM menu ");
        sql.append(" WHERE id = ? ");
        return sql.toString();
    }
    // </editor-fold>
    
    // BEGIN: CRUD
    //<editor-fold defaultstate="expanded" desc="Metodos CRUD.">
    public void buscar() throws SQLException, EntidadeNaoEncontrada {
        //buscar pelo casamento
        getConexao();
        PreparedStatement query = getConexao().prepareStatement(getSQLBuscarPorId());
        query.setInt(1, this.getId()); // id
        ResultSet result = query.executeQuery();
        if (result.next()) {
            this.setId(result.getInt("id"));
            this.setNome(result.getString("nome"));
            this.setIndice(result.getInt("indice"));
        }else{
            throw new EntidadeNaoEncontrada();
        }
    }

    public void salvar() throws SQLException {
        getConexao();
        conexao.setAutoCommit(false);
        inserir();
        conexao.commit();
    }

    protected void inserir() throws SQLException {
        PreparedStatement query = getConexao().prepareStatement(getSQLInsercao(), Statement.RETURN_GENERATED_KEYS);
        query.setString(1, this.getNome()); // nome
        query.setInt(2, this.getIndice()); // indice
        query.executeUpdate();
        ResultSet result = query.getGeneratedKeys();
        result.first();
        int result_id = result.getInt(1);
        this.setId(result_id);
    }

    public void alterar() throws SQLException {
        getConexao();
        conexao.setAutoCommit(false);
        atualizar();
        conexao.commit();
    }

    protected void atualizar() throws SQLException {
        PreparedStatement query = getConexao().prepareStatement(getSQLAtualizacao());
        query.setString(1, this.getNome()); // nome
        query.setInt(2, this.getIndice()); // indice
        query.setInt(3, this.getId()); // id
        query.executeUpdate();
    }

    public void remover() throws SQLException {
        getConexao();
        conexao.setAutoCommit(false);
        excluir();
        conexao.commit();
    }
    
    protected void excluir() throws SQLException{
        removerItensMenu();
        PreparedStatement query = getConexao().prepareStatement(getSQLExclusao());
        query.setInt(1, this.getId()); // id
        query.executeUpdate();
    }
    // </editor-fold>
    // END: CRUD

    private void removerItensMenu() throws SQLException {
        for(ItemMenu item:this.getItensMenu()){
            item.remover();
        }
    }
}
